#include "DLinkNode.h"
status visit(elemtype    c){
        printf("%d", c);
}

/*双向链表初始化*/
status initdlinklist(dlinklist *head, dlinklist* tail){
        (*head)=(dlinklist)malloc(sizeof(node));
        (*tail)=(dlinklist)malloc(sizeof(node));
        if(!(*head)||!(*tail))
                return ERROR;
        /**这一步很关键*/
        (*head)->prior = NULL;
        (*tail)->next = NULL;
        /*链表为空时让头指向尾*/
        (*head)->next=(*tail);
        (*tail)->prior =(*head);
}

/*判定是否为空*/
status emptylinklist(dlinklist head, dlinklist tail){
        if(head->next == tail) {
                return ERROR;
        } else {
                return FALSE;
        }
}

/*尾插法创建链表       */
status createdlinktail(dlinklist head, dlinklist tail, elemtype data){
        dlinklist pmove = tail, pinsert;
        pinsert = (dlinklist)malloc(sizeof(node));
        if(!pinsert) {
                return ERROR;
        }
        pinsert->data=data;
        pinsert->next=NULL;
        pinsert->prior=NULL;
        tail->prior->next=pinsert;
        pinsert->prior=tail->prior;
        pinsert->next = tail;
        tail->prior = pinsert;
}

/*头插法创建链表*/
status createdlinklisthead(dlinklist head, dlinklist tail, elemtype data)
{
        dlinklist pmove = head, qmove = tail, pinsert;
        pinsert=(dlinklist)malloc(sizeof(node));
        if(!pinsert)
                return ERROR;
        else {
                pinsert->data = data;
                pinsert->prior=pmove;
                pinsert->next=pmove->next;
                pmove->next->prior=pinsert;
                pmove->next=pinsert;
        }
}

/*打印链表*/
status printplist(dlinklist head, dlinklist tail){
        /*
        dlinklist pmove = head->next;
        while(pmove!=tail){
                printf("%d ", pmove->data);
                pmove=pmove->next;
        }
        printf("\n");
        return OK;
        */
        dlinklist pmove = head->next;
        while(pmove != tail) {
                visit(pmove->data);
                pmove=pmove->next;
        }
        printf("\n");
}

/*返回第一个值为data的元素的位置*/
status listlength(dlinklist head, dlinklist tail){
        dlinklist pmove = head->next;
        int length=0;
        while(pmove!=tail){
                pmove=pmove->next;
                length++;
        }
        return length;
}

/*删除链表中第pos个位置的元素，并用data返回*/
status deleteelem(dlinklist head, dlinklist tail, int pos, elemtype *data){
        int i=1;
        dlinklist pmove = head->next;
        while(pmove&&i<pos){
                pmove=pmove->next;
                i++;
        }
        if(!pmove || i>pos) {
                printf("输入非法数据");
                return ERROR;
        }
        else {
                *data=pmove->data;
                pmove->next->prior=pmove->prior;
                pmove->prior->next=pmove->next;
                free(pmove);
        }
}

/*在链表尾插入元素*/
status inserttail(dlinklist head, dlinklist tail, elemtype data){
        dlinklist pinsert;
        pinsert=(dlinklist)malloc(sizeof(node));
        pinsert->data = data;
        pinsert->next=NULL;
        pinsert->prior=NULL;
        tail->prior->next=pinsert;
        pinsert->prior=tail->prior;
        pinsert->next=tail;
        tail->prior=pinsert;
        return OK;
}


void testDLinkDemo() {
        dlinklist head, tail;
        int i=0;
        elemtype data = 0;
        initdlinklist(&head, &tail);
        if(emptylinklist(head, tail)) {
                printf("链表为空\n");
        } else {
                printf("链表不为空\n");
        }
        printf("头插法创建链表\n");
        for(i = 0; i<10; i++){
                createdlinklisthead(head, tail, i);
        }
        // traveselist(head, tail);
        for(i = 0; i< 10;i++)
        {
                printf("表中值为%d的元素的位置为", i);
//                printf("%d位\n", locateelem(head, tail, i));
        }
}
